package com.aistar.servlet;

import com.aistar.pojo.Member;
import com.aistar.service.MemberService;
import com.aistar.service.impl.MemberServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "CustomerServlet",urlPatterns = "/customerinfo")
public class CustomerServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.接收页面的用户ID
        String idstr=request.getParameter("custid");

        //2.ID转换成int
        int id =Integer.valueOf(idstr);

        //3.调用service
        MemberService memberService=new MemberServiceImpl();
        Member member=memberService.getById(id);

        //4给出响应数据
        if (member==null){
            //绑定数据,用户不存在响应页面
            request.setAttribute("msg","该用户不存在");
            //forward:相对路径  页面跳转到
            request.getRequestDispatcher("/index.jsp").forward(request,response);
        }else{
            //查到信息显示到对应页面
            request.setAttribute("cust",member);

            // forward : 相对路径 /customer/info.jsp
            request.getRequestDispatcher("/customer/info.jsp").forward(request,response);//页面跳转到customer/info.jsp
            // sendRedirect：绝对路径 /jee_demo/customer/info.jsp
            //response.sendRedirect(request.getContextPath()+"/customer/info.jsp");
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request,response);
    }
}
